import { Actor, HttpAgent } from "@dfinity/agent";
import { hello_backend, idlFactory } from "../../declarations/hello_backend";

function formatDate (d) {
  var now = new Date(parseFloat(d)/1000000);
  var year=now.getFullYear();
  var month=now.getMonth()+1;
  var date=now.getDate();
  if (month >= 1 && month <= 9) {
  month = "0" + month;
  }
  if (date >= 0 && date <= 9) {
  date = "0" + date;
  }
  var hour=now.getHours();
  var minute=now.getMinutes();
  var second=now.getSeconds();
  if (hour >= 1 && hour <= 9) {
  hour = "0" + hour;
  }
  if (minute >= 0 && minute <= 9) {
  minute = "0" + minute;
  }
  if (second >= 0 && second <= 9) {
  second = "0" + second;
  }
  return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
  }
  let date = formatDate(1656630582329)
  console.log(date)  //2022-07-01 07:09:42

async function post(){
 let post_button = document.getElementById("post");
 post_button.disabled = true;
 let textarea = document.getElementById("message");
 let text =textarea.value;

 await hello_backend.post(text);
 textarea.value="";
 post_button.disabled=false;
}



async function load_posts(){
 let posts_section = document.getElementById("posts");
//  new Date().getMilliseconds()
 let posts = await hello_backend.posts(0);
 if (num_posts == posts.length) return;
 posts_section.replaceChildren([]);
 num_posts = posts.length;
 for (var i=0;i < posts.length;i++){
  let post = document.createElement("p");
  post.innerText= posts[i]['author'] + ":\n" +posts[i]['text'] 
  posts_section.appendChild(post)
 }

}

async function load_id_posts(){
  // let id="rrkah-fqaaa-aaaaa-aaaaq-cai"
  let posts_section = document.getElementById("otherposts");
  // new Date().getMilliseconds()
  let posts = await hello_backend.otherposts(this.id,1);
  
  let agent=new HttpAgent();
  // let posts = await Actor.createActor(idlFactory,{agent:agent,canisterId:this.id}).posts(1);
  posts_section.replaceChildren([]);
  let postbutton=posts_section.appendChild(document.createElement("button"))
  let postlabel=posts_section.appendChild(document.createElement("h2"))
  postbutton.innerText = "Close"
  postbutton.onclick = function(){posts_section.replaceChildren([]);}
  postlabel.innerText = this.textContent + "'s Post:"
  for (var i=0;i < posts.length;i++){
   let post = document.createElement("p");
   post.innerText= posts[i]['author'] + ":\n" +posts[i]['text'] 
   posts_section.appendChild(post)
  }
 
 }

var num_follows =0;
async function load_follows(){
  let follows_list = document.getElementById("follows");
  let follows = await hello_backend.follows();
  let follows_name = await hello_backend.follows_name();
  if (num_follows == follows.length) return;
  follows_list.replaceChildren([]);
  for (var i=0;i < follows.length;i++){
    let follow_li = document.createElement("li");
    let link = document.createElement('a');
    let username = follows_name[i];
  if (username==""){
    username = follows[i]
  }
  
  link.textContent = username;
  link.href="javascript:void(0);";
  link.id=follows[i];
  link.onclick = load_id_posts;
  follow_li.appendChild(link);
  follows_list.appendChild(follow_li);
   }
  num_follows = follows.length;
 
 }

var num_posts =0;
async function load_timeline(){
  let timeline_section = document.getElementById("timeline");
  
  let timeline = await hello_backend.timeline(1);
  if (num_posts == timeline.length) return;
  timeline_section.replaceChildren([]);
  
  for (var i=0;i < timeline.length;i++){
   let timelinet = document.createElement("p");
   timelinet.innerText= timeline[i]['author'] +"   update  " + formatDate(timeline[i]['time'])+ ":\n" +timeline[i]['text'];
   timeline_section.appendChild(timelinet)
  }
  num_posts = timeline.length;
 }

function load(){
 let post_button =document.getElementById("post");
 post_button.onclick = post;
 load_follows()
 load_timeline()
 setInterval(load_timeline,5000)
 setInterval(load_follows,5000)
}

window.onload = load

// document.querySelector("form").addEventListener("submit", async (e) => {
//   e.preventDefault();
//   const button = e.target.querySelector("button");

//   const name = document.getElementById("name").value.toString();

//   button.setAttribute("disabled", true);

//   // Interact with foo actor, calling the greet method
//   const greeting = await hello_backend.greet(name);

//   button.removeAttribute("disabled");

//   document.getElementById("greeting").innerText = greeting;

//   return false;
// });
